package com.lu.adog.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ImageFormat;
import android.graphics.Paint;
import android.graphics.Path;
import android.os.Build;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.util.AttributeSet;
import android.view.View;

import com.lu.adog.R;
import com.lu.adog.util.Logg;

/**
 * @author Yue
 * @date 2017/11/30 9:46
 */

public class SortPreView extends View {
    private final static int BAR_RECTANGE = 0;
    private final static int BAR_DOT = 1;
    private final static int[] tempNums = {5, 45, 26, 9, 10, 56};
    private final static float DEFAULT_SPARSENESS = 1.2f;

    private final static int FIT_MODE_NEED = 0;
    private final static int FIT_MODE_ALWAYS = 1;
    private final static int FIT_MODE_NO = 2;

    private final static int AXIS_MODE_WHOLE = 0;
    private final static int AXIS_MODE_ONLY_X = 1;
    private final static int AXIS_MODE_ONLY_Y = 2;
    private final static int AXIS_MODE_HIDE = 3;

    private int barStyle;
    private float barWidth;
    private int[] nums;
    private Paint paint;
    private int barColor;
    private int axisColor;
    private float limitNum;
    private float axisWidth;
    private int autoFit;
    /***
     * 稀疏度，1 则统计bar没有连在一起，越大越稀疏。barWidth = blankWidth / (nums.length * fitSparseness);
     */
    private float fitSparseness;
    private int axisMode;
    private boolean preDefault;

    public SortPreView(Context context) {
        this(context, null);
    }

    public SortPreView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public SortPreView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs, defStyleAttr, 0);
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public SortPreView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init(context, attrs, defStyleAttr, defStyleRes);
    }

    private void init(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        final TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.SortPreView, defStyleAttr, defStyleRes);

        barStyle = a.getInt(R.styleable.SortPreView_barStyle, BAR_RECTANGE);
        limitNum = a.getFloat(R.styleable.SortPreView_limitNum, 100);
        barWidth = a.getDimensionPixelSize(R.styleable.SortPreView_barWidth, 0);
        axisColor = a.getColor(R.styleable.SortPreView_axisColor, Color.GRAY);
        barColor = a.getColor(R.styleable.SortPreView_barColor, Color.GRAY);
        axisWidth = a.getDimensionPixelSize(R.styleable.SortPreView_axisWidth, 0);
        autoFit = a.getInt(R.styleable.SortPreView_autoFit, FIT_MODE_NEED);
        fitSparseness = a.getFloat(R.styleable.SortPreView_fitSparseness, DEFAULT_SPARSENESS);
        axisMode = a.getInt(R.styleable.SortPreView_axisMode,AXIS_MODE_WHOLE);
        preDefault = a.getBoolean(R.styleable.SortPreView_preDefault,true);

        a.recycle();

        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        if (preDefault) {
            nums = tempNums;
        }else {
            nums = new int[]{};
        }

    }

    public float getAxisWidth() {
        return axisWidth;
    }

    public void setAxisWidth(float axisWidth) {
        this.axisWidth = axisWidth;
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        fixBarWidth();
    }

    private void fixBarWidth() {
        if (autoFit == FIT_MODE_NO) {
            return;
        }

        float width = getMeasuredWidth();
        float blankWidth = width - axisWidth;

        if (autoFit == FIT_MODE_ALWAYS) {
                barWidth = blankWidth / (nums.length * fitSparseness);
        } else {//需要时，自适应

            float expectWidth = axisWidth + barWidth * nums.length;

            if (expectWidth > width || barWidth == 0) {//期待的宽大于view的宽,或者未设宽度
                barWidth = blankWidth / (nums.length * fitSparseness);
            }
        }

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        drawAxis(canvas);
        drawSortBar(canvas);
    }


    private void drawAxis(Canvas canvas) {
        if (axisMode == AXIS_MODE_HIDE){
            return;
        }
        Path path = new Path();

        if (axisMode == AXIS_MODE_ONLY_X){
            path.moveTo(0,getHeight()-axisWidth);
            path.lineTo(0,getHeight());
            path.lineTo(getWidth(),getHeight());
            path.lineTo(getWidth(),getHeight()-axisWidth);

        }else if (axisMode == AXIS_MODE_ONLY_Y){

            path.lineTo(axisWidth, 0);
            path.lineTo(axisWidth, getHeight() - axisWidth);
            path.lineTo(0,getHeight() - axisWidth);
            path.lineTo(0,0);

        }else{//whole
            path.lineTo(axisWidth, 0);
            path.lineTo(axisWidth, getHeight() - axisWidth);
            path.lineTo(getWidth(), getHeight() - axisWidth);
            path.lineTo(getWidth(), getHeight());
            path.lineTo(0, getHeight());
            path.lineTo(0, 0);
        }
        path.close();
        paint.setColor(axisColor);


        canvas.drawPath(path, paint);

    }

    private void drawSortBar(Canvas canvas) {

        if (nums == null){
            return;
        }

        fixBarWidth();

        paint.setTextSize(barWidth/3);
        Paint.FontMetrics fontMetrics = paint.getFontMetrics();
        float textHeight = fontMetrics.descent - fontMetrics.ascent;

        float blankWidth = getWidth() - axisWidth;
        float blankHeight = getHeight() - axisWidth - textHeight;

        float everyY = blankHeight / limitNum;
        float everyX = blankWidth / nums.length;

        float barHeight;


        float barLeft;
        float barRight;
        float barTop;
        float barBottom;



        for (int i = 0; i < nums.length; i++) {
            int num = nums[i];

            barHeight = num * everyY;

            barTop = blankHeight - barHeight;
            barLeft = everyX * i + (everyX - barWidth) / 2f + axisWidth;
            barRight = barLeft + barWidth;

            if (barStyle == BAR_DOT) {
                barBottom = barTop + barWidth;
            } else {
                barBottom = blankHeight;
            }
            barTop +=  textHeight;
            barBottom +=  textHeight;
            canvas.drawRect(barLeft, barTop, barRight, barBottom, paint);

            String text = num + "";
            float textWidth = paint.measureText(text);
            float textX = barLeft + (barWidth - textWidth) / 2f;


            float textY = barTop - (textHeight) / 2f;

            canvas.drawText(text, textX, textY, paint);
        }
    }


    public int[] getNums() {
        return nums;
    }

    public void setNums(int[] nums) {
        this.nums = nums;
    }

    public int getBarStyle() {
        return barStyle;
    }

    public void setBarStyle(int barStyle) {
        this.barStyle = barStyle;
    }


    public float getBarWidth() {
        return barWidth;
    }

    public void setBarWidth(float barWidth) {
        this.barWidth = barWidth;
    }

    public int getBarColor() {
        return barColor;
    }

    public void setBarColor(int barColor) {
        this.barColor = barColor;
    }

    public int getAxisColor() {
        return axisColor;
    }

    public void setAxisColor(int axisColor) {
        this.axisColor = axisColor;
    }

    public float getLimitNum() {
        return limitNum;
    }

    public void setLimitNum(float limitNum) {
        this.limitNum = limitNum;
    }

    public void update() {
        if (limitNum == 0) {
            limitNum = 100;
        }
        if (nums == null) {
            if (preDefault) {
                nums = tempNums;
            }
        }
        invalidate();
    }

}
